package net.learn.system.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import net.learn.framework.common.page.PageResult;
import net.learn.framework.common.utils.Result;
import net.learn.system.convert.SysNewConvert;
import net.learn.system.entity.SysNewEntity;
import net.learn.system.query.SysNewQuery;
import net.learn.system.service.SysNewService;
import net.learn.system.vo.SysNewVO;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;


/**
* 新闻管理
*/
@RestController
@RequestMapping("sys/new")
@Tag(name="新闻管理")
@AllArgsConstructor
public class SysNewController {
    private final SysNewService sysNewService;

    @GetMapping("page")
    @Operation(summary = "分页")
    public Result<PageResult<SysNewVO>> page(@Valid SysNewQuery query){
        PageResult<SysNewVO> page = sysNewService.page(query);

        return Result.ok(page);
    }
    @GetMapping("{id}")
    @Operation(summary = "信息")
    @PreAuthorize("hasAuthority('sys:new:info')")
    public Result<SysNewVO> get(@PathVariable("id") Long id){
        SysNewEntity entity = sysNewService.getById(id);

        return Result.ok(SysNewConvert.INSTANCE.convert(entity));
    }
    @GetMapping("get")
    @Operation(summary = "详细信息")
    public Result<SysNewVO> get(@Valid SysNewQuery query){
        SysNewEntity entity = sysNewService.getById(query.getId());

        return Result.ok(SysNewConvert.INSTANCE.convert(entity));
    }
    @PostMapping("list")
    @Operation(summary = "列表")
    @PreAuthorize("hasAuthority('sys:new:list')")
    public Result<List<SysNewVO>> list(@RequestBody @Valid SysNewQuery query){
        List<SysNewVO> list = sysNewService.getList(query);

        return Result.ok(list);
    }
    @PostMapping("save")
    @Operation(summary = "保存")
    @PreAuthorize("hasAuthority('sys:new:save')")
    public Result<String> save(@RequestBody SysNewVO vo){
        sysNewService.save(vo);

        return Result.ok();
    }

    @PutMapping
    @Operation(summary = "修改")
    @PreAuthorize("hasAuthority('sys:new:update')")
    public Result<String> update(@RequestBody @Valid SysNewVO vo){
        sysNewService.update(vo);

        return Result.ok();
    }

    @DeleteMapping
    @Operation(summary = "删除")
    @PreAuthorize("hasAuthority('sys:new:delete')")
    public Result<String> delete(@RequestBody List<Long> idList){
        sysNewService.delete(idList);

        return Result.ok();
    }
}